import requests


def get_package_versions(package_name, index_url="https://pypi.org/simple"):
    """
    通过 PyPI JSON API 获取包的版本列表
    :param package_name: 包名称，例如 'numpy'
    :param index_url: 镜像源地址，默认为 PyPI 官方源
    :return: 包版本列表
    """
    try:
        # 构造 JSON API 的 URL
        api_url = f"{index_url.rstrip('/')}/pypi/{package_name}/json"
        response = requests.get(api_url)
        response.raise_for_status()  # 检查请求是否成功
        data = response.json()

        # 提取版本列表
        versions = list(data["releases"].keys())
        return versions
    except requests.RequestException as e:
        print(f"请求失败：{e}")
        return []
    except KeyError:
        print(f"未找到包 {package_name} 的版本信息。")
        return []


# 示例调用
package_name = "numpy"
index_url = "https://pypi.tuna.tsinghua.edu.cn/simple"
versions = get_package_versions(package_name, index_url)

if versions:
    print(f"可用的 {package_name} 版本列表：")
    for version in versions:
        print(version)
else:
    print(f"未找到 {package_name} 的版本信息。")